#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::Maipu::ShowIpInterfaceBrief < Aio::Module::Cmd::Maipu
  include Aio::Module

  def initialize
    super({
      :cmd_full			=> "show ip interface brief",
      :cmd_short		=> "sh ip int b",
      :author				=> "Elin",
      :description	=> "This is Maipu Command# show ip interface brief",
      :ranking			=> Ranking_1,
      :platform			=> RS,
      :benchmark	=> { }
    })
  end

  def parse
    context = self.context.dup
    useful[:interface_brief] = {}
    context.readline_match_block(/Interface IP-Address Status Protocol Description/) {|b|b}
    context.readline_match_block_loop(/(?<interface_id>[^\s]+) (?<ip>[^\s]+) (?<status>[^\s]+) (?<protocol_description>[^\s].*)/, reg_blank) do |block|
      iface = {}
      iface_id = block[:interface_id]
      useful[:interface_brief][iface_id.to_sym] = iface
      
      protocol, description = block[:protocol_description].split(' ', 2)
      block.update(iface, :interface_id)
      block.update(iface, :ip)
      block.update(iface, :status)
      block.update(iface, :protocol, protocol)
      block.update(iface, :description, description.to_s)
      # 当状态是起来的,但是协议为Down,则纳入警告
      if (block[:status] =~ /up/) and !(protocol =~ /up/)
        block.warning_serious(iface, :protocol, self, {string: protocol, force: true})
      else
        block.warning_serious(iface, :protocol, self, {string: protocol, force: false})
      end
    end
  end
end

